<?php
namespace app\index\controller;

use app\index\model\User;
use app\index\model\MobileSend;
use think\Request;
use think\Session;

class Login extends Common
{
    public function index()
    {
        \think\Session::set('cps_user_id', 1);
        \think\Session::set('cps_user_name', '张三');
        \think\Session::set('cps_user_head_img', 'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=4212705485,2141668227&fm=27&gp=0.jpg');
        \think\Session::set('cps_user_mobile', '13501334050');
        return $this->fetch();
    }

    //去授权
    public function login()
    {
        $userModel = new User();
        $userModel->tuAuth();
    }

    //授权获取用户信息
    public function dologin()
    {
        $userModel = new User();
        $return = $userModel->doLogin();
        if ($return['status'] === 0) {
            $this->error($return['msg']);
        }
        $this->success('登录成功', '/index');
    }

    //绑定手机号
    public function bind_mobile()
    {
        if (Session::get('cps_user_mobile')) {
            $this->redirect('index/index');
        }
        return $this->fetch();
    }

    //发送验证码
    public function send_code(Request $request)
    {
        if ($request->isPost()) {
            $mobile = $request->post('mobile', '', 'filterString');
            $userModel = new User();
            if (!preg_match('/^1[3|4|5|7|8][0-9]{9}$/', $mobile)) {
                $this->error('手机号错误');
            } elseif ($userModel->getInfoByField('mobile', $mobile)) {
                $this->error('该手机号已被注册');
            }
            $return = $userModel->send_code($mobile);
            if ($return['status'] === 0) {
                $this->error($return['msg']);
            }
            $this->success('发送成功', '/index');
        }
    }
    //绑定手机号
    public function to_bind_mobile(Request $request)
    {
        if ($request->isPost()) {
            $username = $request->post('username', '', 'filterString');
            $mobile = $request->post('mobile', '', 'filterString');
            $code = $request->post('code', 0, 'intval');
            $userModel = new User();
            $mobileSendModel = new MobileSend();
            if (!$username) {
                $this->error('请输入您的姓名');
            } elseif (mb_strlen($username) > 50) {
                $this->error('您的姓名过长');
            } elseif (!preg_match('/^1[3|4|5|7|8][0-9]{9}$/', $mobile)) {
                $this->error('手机号错误');
            } elseif (!$code) {
                $this->error('请输入验证码');
            } elseif ($userModel->getInfoByField('mobile', $mobile)) {
                $this->error('该手机号已被注册');
            }
            //检测验证码
            $return = $mobileSendModel->checkCode($mobile, $code);
            if ($return['status'] === 0) {
                $this->error($return['msg']);
            }
            //绑定手机号
            if (!$userModel->bindMobile($username, $mobile)) {
                $this->error('注册失败,请稍后再试');
            }
            $this->success('注册成功', '/index');
        }
    }

    //退出登录
    public function login_out()
    {
        session('cps_user_id', null);
        session('cps_user_name', null);
        session('cps_user_status', null);
        session('cps_user_head_img', null);
        session('cps_user_mobile', null);
        $this->success('退出成功', '/index/login');
    }
}
